package com.service;

import static com.def.GenericDef.*;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.Map.Entry;

import com.dao.*;
import com.model.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.icim.service.ICIMBaseService;
import com.mapKeyVal.key.StatBoxOpeBnkWoKey;
import com.mapKeyVal.key.StatBoxOpeBnkWoMtrlKey;
import com.mapKeyVal.key.WoOpeKey;
import com.rtnBean.BoxInfoBeen;
import com.rtnBean.NotFoundRtnBeen;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Xpinqbox.XpinqboxI;
import com.tx.Xpinqbox.XpinqboxIA;
import com.tx.Xpinqbox.XpinqboxO;
import com.tx.Xpinqbox.XpinqboxO1;
import com.tx.Xpinqbox.XpinqboxO2;
import com.tx.Xpinqbox.XpinqboxO3;
import com.tx.Xpinqbox.XpinqboxO4;
import com.tx.Xpinqbox.XpinqboxO5;
import com.tx.Xpinqpalt.XpinqpaltOB;
import com.util.HCutil;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpinqboxService")
public class XpinqboxService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpinqboxService.class);
	private static String TX_NAME = T_XPINQBOX;
	
	private XpinqboxI inTrx;
	private XpinqboxO outTrx;

	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IRetWODao retWoDao;
	@Autowired
	private IRetPrdJgeDao retPrdJgeDao;
	@Autowired
	private IRetPrdIndtDao retPrdIndtDao;
	@Autowired
	private IRetPrdInDao retPrdInDao;
	@Autowired
	private IBisPathiDao bisPathiDao;
	@Autowired
	private IBisOpeDao bisOpeDao;
	@Autowired
	private IBisPathDao bisPathDao;
	@Autowired
	private IRetCusPrdInDao retCusPrdInDao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	
	private String gszMsgBuf = "";

	public String subMainProc(String strInMsg) {
		long rtnCode = _NORMAL;
		gszMsgBuf = "";
		String outMsg = "";
		inTrx = new XpinqboxI();
		outTrx = new XpinqboxO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		MqOpe mqOpe = new MqOpe();

		try {
			inTrx = (XpinqboxI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2(inTrx.getAction_flg().toCharArray()[0]);
			if (rtnCode != _NORMAL) {
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				retPrdInfoDao.flushCurSession();
			}
		} catch (Exception ex) {
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			logger.info(outMsg);
		}
		return outMsg;
	}

	private long subMainProc2(char actionType) {
		long rtnCode = _NORMAL;

		switch (actionType) {
		case 'A':{
			rtnCode = getBoxInfofor4C00();
			break;
		}
		case 'B':{
			rtnCode = getPrdList();
			break;
		}
		case 'C': {
			rtnCode = getBoxtableInfoByCondition();
			break;
		}
		case 'D': {// 查询箱子中产品重工次数
			rtnCode = queryPrdRwCnt();
			break;
		}
		case 'E':{
			rtnCode = getPrdInfoList();
			break;
		}
		case 'F': {
			rtnCode = getBoxtableInfoByCondition1();
			break;
		}
		case 'H':{// 变更ret_box和ret_prd_info中prd_admin_flg栏位第一码
			rtnCode = changePrdAdminFlg();
			break;
		}
		case 'I': {
			rtnCode = queryBoxInfo2CusObj();
			break;
		}
		case 'L':{
			rtnCode = getLwInfo();
			break;
		}
		case 'M': {
			rtnCode = summaryBoxInfoFor9100();
			break;
		}
		case 'P': {
			rtnCode = getAllPrdInBoxInfo2();
			break;
		}
		case 'Q': {
			rtnCode = getBoxInfo();
			break;
		}
		case 'R': {// 查询在制保留的箱子和产品信息
			rtnCode = queryRetainInfo();
			break;
		}
		case 'S': {
			rtnCode = getAllPrdInBoxInfo();
			break;
		}
		case 'T': {
			rtnCode = getBoxtableInfo();
			break;
		}
		case 'W': {
			rtnCode = getBoxInfoByWO();
			break;
		}
		case 'X': {// 查询箱子中产品的来料箱信息，及是否混批次
			rtnCode = queryBoxInfo();
			break;
		}
		case 'Y': {
			rtnCode = setBoxYield();
			break;
		}
		case 'G': {
			rtnCode = getBoxInfoByShipBox();
			break;
		}
		default: {
			gszMsgBuf = "Invalide type input: [" + actionType + "]";
			return E_XPINQBOX_INVALID_INPUT;
		}
		}
		return rtnCode;
	}

	private long setBoxYield() {
		long lRc = _NORMAL;
		Ret_box box = new Ret_box();
		box = retBoxDao.get(inTrx.getBox_id());
		if (null == box) {
			gszMsgBuf = new NotFoundRtnBeen(inTrx.getBox_id(), Ret_box.class)
					.toString();
			return E_RET_BOX + E_READ_NOT_FOUND;
		}

		box = retBoxDao.setBoxYield(box);
		logger.info("Set box [" + box.getBox_id() + "] fst_yield ["
				+ box.getBox_fst_yield() + "], snd_yield ["
				+ box.getBox_snd_yield() + "]");

		outTrx.setRet_box(box);

		retBoxDao.update(box);

		return lRc;
	}

	/**
	 * 查询箱子信息，返回BoxInfoBeen类型
	 * 
	 * @return
	 */
	private long queryBoxInfo2CusObj() {
		List<BoxInfoBeen> boxInfoBeenList = new ArrayList<BoxInfoBeen>();

		if (_YES.equals(inTrx.getWip_bank_flg())) {
				boxInfoBeenList = retBoxDao.retBox2BoxInfoByManyConditionWipWHwithFAB(
						inTrx.getBox_id(), inTrx.getBox_stat(), inTrx.getWo_id(),
						inTrx.getDest_shop(), WIP_BANK_IN, inTrx.getOpe_id());
		} else if (_NO.equals(inTrx.getWip_bank_flg())) {
				boxInfoBeenList = retBoxDao.retBox2BoxInfoByManyConditionWipWHwithFAB(
						inTrx.getBox_id(), inTrx.getBox_stat(), inTrx.getWo_id(),
						inTrx.getDest_shop(), BANK_OUT, inTrx.getOpe_id());
		} else {
			if (Util.isSpaceCheck(inTrx.getBnk_flg())) {
				boxInfoBeenList = retBoxDao.retBox2BoxInfoByManyConditionWHwithSHOP(
						inTrx.getBox_id(), inTrx.getBox_stat(),
						inTrx.getWo_id(), inTrx.getWo_ids(),
						inTrx.getDest_shop(), inTrx.getBnk_flg(),
						inTrx.getCus_id(), inTrx.getSo_id());
			} else {
				boxInfoBeenList = retBoxDao.retBox2BoxInfoByManyConditionWHwithFAB(
						inTrx.getBox_id(), inTrx.getBox_stat(),
						inTrx.getWo_id(), inTrx.getWo_ids(),
						inTrx.getDest_shop(), inTrx.getBnk_flg(),
						inTrx.getCus_id(), inTrx.getSo_id());

			}
		}
		if (!boxInfoBeenList.isEmpty()) {
			outTrx.setBox_info(boxInfoBeenList);
			outTrx.setBox_cnt(boxInfoBeenList.size());
		}
		return _NORMAL;
	}

	/**
	 * 根据箱号查询箱子信息，返回整表数据
	 * 
	 * @return
	 */
	private long getBoxtableInfo() {
		long lRc = _NORMAL;
		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			gszMsgBuf = "Input box id [" + inTrx.getBox_id() + "] is null!";
			return E_XPINQBOX_INVALID_INPUT_BOX_ID;
		}

		Ret_box box = new Ret_box();
		box = retBoxDao.get(inTrx.getBox_id());
		if (null == box) {
			gszMsgBuf = new NotFoundRtnBeen(inTrx.getBox_id(), Ret_box.class)
					.toString();
			return E_RET_BOX + E_READ_NOT_FOUND;
		}

		outTrx.setRet_box(box);
		outTrx.setBox_cnt(1);

		return lRc;
	}

	/**
	 * 查询箱子信息，返回符合条件的箱子表
	 * 
	 * @return
	 */
	private long getBoxtableInfoByCondition() {
		long lRc = _NORMAL;
		String sql = "FROM Ret_box WHERE 1=1 ";

		if (!Util.isSpaceCheck(inTrx.getBox_id())) {
			sql += " AND box_id='" + inTrx.getBox_id() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getBox_stat())) {
			sql += " AND box_stat='" + inTrx.getBox_stat() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getBnk_flg())) {
			sql += " AND bnk_flg = '" + inTrx.getBnk_flg() + "' ";
		}
		if (!Util.isSpaceCheck(inTrx.getWo_id())) {
			sql += " AND wo_id_fk = '" + inTrx.getWo_id() + "' ";
		}
		if (!Util.isSpaceCheck(inTrx.getTool_id())) {
			sql += " AND tool_id_fk = '" + inTrx.getTool_id() + "' ";
		}
		if(!Util.isSpaceCheck(inTrx.getDest_shop())){
			sql += " AND dest_shop ='" + inTrx.getDest_shop() +"' ";
		}
		if(!Util.isSpaceCheck(inTrx.getEvt_cate())){
			sql += " AND evt_cate ='" + inTrx.getEvt_cate() +"' ";
		}
		if (!Util.isSpaceCheck(inTrx.getLot_id())) {
			sql += "AND lot_id_fk like ('%" + inTrx.getLot_id() + "%') ";
		}
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		boxList = retBoxDao.find(sql);
		outTrx.setBox_cnt(0);
		if (null == boxList) {
			outTrx.setBox_cnt(0);
		} else {
			outTrx.setBox_cnt(boxList.size());
			outTrx.setOary_box(boxList);
		}

		return lRc;
	}
	private long getBoxtableInfoByCondition1() {
		long lRc = _NORMAL;
		String sql = "FROM Ret_box WHERE 1=1 AND evt_cate <>'WHCX'";

		if (!Util.isSpaceCheck(inTrx.getBox_id())) {
			sql += " AND box_id='" + inTrx.getBox_id() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getBox_stat())) {
			sql += " AND box_stat='" + inTrx.getBox_stat() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getBnk_flg())) {
			sql += " AND bnk_flg = '" + inTrx.getBnk_flg() + "' ";
		}
		if (!Util.isSpaceCheck(inTrx.getWo_id())) {
			sql += " AND wo_id_fk = '" + inTrx.getWo_id() + "' ";
		}
		if(!Util.isSpaceCheck(inTrx.getDest_shop())){
			sql += " AND dest_shop ='" + inTrx.getDest_shop() +"' ";
		}
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		boxList = retBoxDao.find(sql);
		outTrx.setBox_cnt(0);
		if (null == boxList) {
			outTrx.setBox_cnt(0);
		} else {
			outTrx.setBox_cnt(boxList.size());
			outTrx.setOary_box(boxList);
		}

		return lRc;
	}
	/**
	 * Query boxes info by wo_id
	 * 
	 * @return
	 */
	private long getBoxInfoByWO() {
		long lRc = _NORMAL;
		String queryBoxStat = PROC_WAIT;

		if (Util.isSpaceCheck(inTrx.getWo_id())) {
			gszMsgBuf = "输入的工单代码为空!";
			return E_XPINQBOX_INVALID_INPUT_WO_ID;
		}
		if (!Util.isSpaceCheck(inTrx.getBox_stat())) {
			queryBoxStat = inTrx.getBox_stat();
		}

		String sql = "FROM Ret_box WHERE BOX_STAT='" + queryBoxStat
				+ "' AND WO_ID_FK='" + inTrx.getWo_id() + "' ";
		// if(_YES.equals(inTrx.getWip_bank_flg())){
		// sql = sql + " AND dest_shop = '"+ DEST_SHOP_WIP +"'";
		// }
		if (!Util.isSpaceCheck(inTrx.getDest_shop())) {
			sql += " AND dest_shop = '" + inTrx.getDest_shop() + "' ";
		}
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		boxList = retBoxDao.find(sql);
		outTrx.setBox_cnt(0);
		if (null == boxList) {
			outTrx.setBox_cnt(0);
		} else {
			outTrx.setBox_cnt(boxList.size());
			outTrx.setOary_box(boxList);
		}

		return lRc;
	}

	private long getAllPrdInBoxInfo() {
		long lRc = _NORMAL;

		// Get box info
		lRc = getBoxInfo();
		if (_NORMAL != lRc) {
			return lRc;
		}

		// Query prd list in box and set to oary
		lRc = setOutTrxByPrdList(inTrx.getBox_id());
		if (_NORMAL != lRc) {
			return lRc;
		}

		return lRc;
	}

	private long getAllPrdInBoxInfo2() {
		long lRc = _NORMAL;
		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			gszMsgBuf = "输入的箱号为空!";
			return E_XPINQBOX_INVALID_INPUT_BOX_ID;
		}

		Ret_box box = new Ret_box();
		box = retBoxDao.get(inTrx.getBox_id());
		if (null == box) {
			outTrx.setBox_cnt(0);
			return lRc;
		} else {
			outTrx.setBox_id(box.getBox_id());
			outTrx.setLot_id_fk(box.getLot_id_fk());
			outTrx.setBox_stat(box.getBox_stat());
			outTrx.setValid_flg(box.getValid_flg());
			outTrx.setGroup_id(box.getGroup_id());
			outTrx.setPpbox_id(box.getPpbox_id());
			outTrx.setReal_emp(box.getReal_emp());
			outTrx.setBox_use_cnt(box.getBox_use_cnt());
			outTrx.setPrd_grade_fk(box.getPrd_grade_fk());
			outTrx.setStd_qty(box.getStd_qty());
			outTrx.setPrd_qty(box.getPrd_qty());
			outTrx.setMdl_typ_fk(box.getMdl_typ_fk());
			outTrx.setMdl_cate_fk(box.getMdl_cate_fk());
			outTrx.setMdl_id_fk(box.getMdl_id_fk());
			outTrx.setMdl_ec_code_fk(box.getMdl_ec_code_fk());
			outTrx.setMtrl_prod_id_fk(box.getMtrl_prod_id_fk());
			outTrx.setAct_cmp_timestamp(box.getAct_cmp_timestamp());
			outTrx.setWh_in_timestamp(box.getWh_in_timestamp());
			outTrx.setPrty(box.getPrty());
			outTrx.setPv_prty(box.getPv_prty());
			outTrx.setOqc_lotid(box.getOqc_lotid());
			outTrx.setOqc_skip_flg(box.getOqc_skip_flg());
			outTrx.setWo_id_fk(box.getWo_id_fk());
			Ret_wo ret_wo = retWoDao.get(box.getWo_id_fk());
			if(ret_wo == null){
				outTrx.setCus_id_fk("");
			}else{
				outTrx.setCus_id_fk(ret_wo.getCus_id_fk());
			}

			outTrx.setBox_cnt(1);
			// Query prd list in box and set to oary
			lRc = setOutTrxByPrdList(inTrx.getBox_id());
			if (_NORMAL != lRc) {
				return lRc;
			}
		}
		return lRc;
	}

	/**
	 * Only query box info
	 * 
	 * @return
	 */
	private long getBoxInfo() {
		long lRc = _NORMAL;
		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			gszMsgBuf = "Input box id [" + inTrx.getBox_id() + "] is null!";
			return E_XPINQBOX_INVALID_INPUT_BOX_ID;
		}

		Ret_box box = new Ret_box();
		box = retBoxDao.get(inTrx.getBox_id());
		if (null == box) {
			gszMsgBuf = new NotFoundRtnBeen(inTrx.getBox_id(), Ret_box.class).toString();
			return E_RET_BOX + E_READ_NOT_FOUND;
		}

		seOutTrxByRetBox(box);
		Ret_cus_prd_in cusPrdIn = retCusPrdInDao.get(box.getBox_id());
		if(cusPrdIn != null){
			outTrx.setBox_phy_id(cusPrdIn.getBox_phy_id());
		}
		if(!Util.isSpaceCheck(box.getWo_id_fk())){
			Ret_wo ret_wo = new Ret_wo();
			ret_wo = retWoDao.get(box.getWo_id_fk());
			if(ret_wo != null){
				outTrx.setMdl_cate_fk(ret_wo.getMdl_cate_fk());
				outTrx.setCus_id_fk(ret_wo.getCus_id_fk());
				outTrx.setTo_thickness(ret_wo.getTo_thickness());
				outTrx.setT_thickness(ret_wo.getT_thickness());
				outTrx.setC_thickness(ret_wo.getC_thickness());
				outTrx.setCus_info_fst(ret_wo.getCus_info_fst());
				outTrx.setCus_info_snd(ret_wo.getCus_info_snd());
				outTrx.setFm_mdl_id_fk(ret_wo.getFm_mdl_id_fk());
				outTrx.setTh_mdl_id_fk(ret_wo.getTh_mdl_id_fk());
				outTrx.setCus_mtrl_id_fk(ret_wo.getCus_mtrl_id_fk());
				outTrx.setWo_cate(ret_wo.getWo_cate());
				outTrx.setMainT(ret_wo.getMaint());
				outTrx.setShtT(ret_wo.getShtt());
				outTrx.setWo_dsc(ret_wo.getWo_dsc());
				outTrx.setJgyq(ret_wo.getJgyq());
				DateFormat pltime = new SimpleDateFormat("yyyy/MM/dd");
				outTrx.setPln_cmp_date(pltime.format(ret_wo.getPln_cmp_date()));
				outTrx.setFhdir(ret_wo.getFhdir());
				Bis_mdl_def mdldef = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
				if(null != mdldef){
					outTrx.setGuige(mdldef.getMdl_typ() + "*" + mdldef.getTo_thickness());
				}
				String hql = "FROM Bis_data where data_cate=? and data_item=?";
				Bis_data bis_data = bisDataDao.uniqueResult(hql, "CUSD",ret_wo.getCus_id_fk());
				if(null!=bis_data){
					outTrx.setCus_dsc(bis_data.getData_ext());
				}
			}
		}
		if(!Util.isSpaceCheck(box.getSo_id_fk())){
			Ret_wo ret_so = new Ret_wo();
			ret_so = retWoDao.get(box.getSo_id_fk());
			if(ret_so != null){
				outTrx.setAc_so_id(ret_so.getSo_id());
			}
		}
		List<Ret_prd_info> ret_prd_infoList = retPrdInfoDao.getShtListInfo(inTrx.getBox_id());

		if (ret_prd_infoList != null ){
			if (!ret_prd_infoList.isEmpty()) {
				outTrx.setOary(ret_prd_infoList);
			}
		}
		
		return lRc;
	}

	/**
	 * Query prd list in box and set to oary
	 */
	private long setOutTrxByPrdList(String boxID) {
		long lRc = _NORMAL;

		List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
		if (_YES.equals(inTrx.getOrder_slot_flg())) {
			ret_prd_infoList = retPrdInfoDao.getShtListInfoOrderBySlotDesc(boxID);
		} else {
			if (inTrx.getWo_id() != null && !Util.isSpaceCheck(inTrx.getWo_id()) ) {
				ret_prd_infoList = retPrdInfoDao.getShtListInfoWithWo(boxID, inTrx.getWo_id());
			}else {
				ret_prd_infoList = retPrdInfoDao.getShtListInfo(boxID);
			}
		}
		if (ret_prd_infoList != null ){
			if (!ret_prd_infoList.isEmpty()) {
				outTrx.setOary(ret_prd_infoList);
				
			}
		}
		if (!Util.isSpaceCheck(inTrx.getFirst_ope_id())) {
			for (Ret_prd_info prd : ret_prd_infoList) {
				String path_id = prd.getCr_path_id_fk();
				String path_ver = prd.getCr_path_ver_fk();
				
				if (Util.isSpaceCheck(path_id) || Util.isSpaceCheck(path_ver)) {
					gszMsgBuf = "该面板["+prd.getPrd_seq_id()+"]工艺线路信息不存在";
					return E_XPINQBOX_INVALID_INPUT_BOX_ID;
				}
				
				Bis_pathiId bis_pathiId = new Bis_pathiId();
				bis_pathiId.setPath_id(path_id);
				bis_pathiId.setPath_ver(path_ver);
				
				Bis_pathi bis_pathi = new Bis_pathi();
				bis_pathi = bisPathiDao.get(bis_pathiId);
				if (null == bis_pathi) {
					gszMsgBuf = "该工艺线路不存在"+path_id;
					return E_XPINQBOX_INVALID_INPUT_BOX_ID;
				}

				//这里的应该找ope_id
				String firstOpeNo =  bis_pathi.getFirst_ope_no();
				Bis_pathId pathId = new Bis_pathId();
				pathId.setPath_id_fk(path_id);
				pathId.setPath_ver_fk(path_ver);
				pathId.setCr_ope_no(firstOpeNo);
				Bis_path bis_path = bisPathDao.get(pathId);
				outTrx.setFirst_ope_id(bis_path.getCr_ope_id_fk());
				break;
			}
		}

		return lRc;
	}

	private void setGradeCntOfOutTrx(String box_id) {
		int okCnt, ngCnt, gkCnt, lzCnt;
		okCnt = ngCnt = gkCnt = lzCnt = 0;
		List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
		ret_prd_infoList = retPrdInfoDao.getShtListInfo(box_id);
		if (null != ret_prd_infoList) {
			for (Ret_prd_info prd : ret_prd_infoList) {
				if (_OK.equals(prd.getPrd_grade())) {
					okCnt++;
				} else if (_NG.equals(prd.getPrd_grade())) {
					ngCnt++;
				} else if (_GK.equals(prd.getPrd_grade())) {
					gkCnt++;
				} else if (_LZ.equals(prd.getPrd_grade())) {
					lzCnt++;
				}
			}
			
			outTrx.setOk_prd_qty(okCnt);
			outTrx.setNg_prd_qty(ngCnt);
			outTrx.setGk_prd_qty(gkCnt);
			outTrx.setLz_prd_qty(lzCnt);
		}
	}

	/**
	 * Set outTrx info by ret_box object
	 * 
	 * @param box
	 */
	private long seOutTrxByRetBox(Ret_box box) {
		if (!Util.isSpaceCheck(inTrx.getWh_out_flg()) && _YES.equals(inTrx.getWh_out_flg())) {
			// yield
			Double box_yield = 0.0;
			Double okPrdCnt = 0.0;
			Double totalCnt = box.getPrd_qty().doubleValue();
			if (totalCnt > 0) {
				setGradeCntOfOutTrx(box.getBox_id());
//				okPrdCnt = (double) retPrdInfoDao.getOkPrdCnt(box.getBox_id());
				box_yield = outTrx.getOk_prd_qty() / totalCnt;
				BigDecimal b1 = new BigDecimal(Double.toString(okPrdCnt));
				BigDecimal b2 = new BigDecimal(Double.toString(totalCnt));
				box_yield = b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
				outTrx.setBox_yield(box_yield);
			}

			// Proc pass check 
			//150807检查为未使用，暂时Mark掉 Mosie
//			int jb_pass_index = -1;
//			int dm_pass_index = -1;
//			Bis_data data_PRST_JBSK = new Bis_data();
//			Bis_data data_PRST_DMGY = new Bis_data();
//
//			data_PRST_JBSK = (bisDataDao.queryBisData("PRST", "JBSK", null,
//					null, null, null, null, null)).get(0);
//			if (null == data_PRST_JBSK) {
//				gszMsgBuf = new NotFoundRtnBeen("[PRST-JBSK]", Bis_data.class).toString();
//				return E_BIS_DATA + E_READ_NOT_FOUND;
//			}
//
//			data_PRST_DMGY = (bisDataDao.queryBisData("PRST", "DMGY", null,
//					null, null, null, null, null)).get(0);
//			if (null == data_PRST_DMGY) {
//				gszMsgBuf = new NotFoundRtnBeen("[PRST-DMGY]", Bis_data.class).toString();
//				return E_BIS_DATA + E_READ_NOT_FOUND;
//			}
//
//			jb_pass_index = Integer.valueOf(data_PRST_JBSK.getExt_2());
//			dm_pass_index = Integer.valueOf(data_PRST_DMGY.getExt_2());
//
//			if (jb_pass_index > 0 && dm_pass_index > 0) {
//				List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
//				ret_prd_infoList = retPrdInfoDao.getShtListInfo(box.getBox_id());
//				if (null != ret_prd_infoList) {
//					String jb_pass_flg = _NO;
//					String dm_pass_flg = _NO;
//					boolean jd_chking = true; // Check not finish
//					boolean dm_chking = true;
//					char jb_0 = ret_prd_infoList.get(0).getCus_sn().toCharArray()[jb_pass_index - 1];
//					char dm_0 = ret_prd_infoList.get(0).getCus_sn().toCharArray()[dm_pass_index - 1];
//
//					for (int i = 1, length = ret_prd_infoList.size(); i < length; i++) {
//						if (jd_chking && jb_0 != ret_prd_infoList.get(i).getCus_sn().toCharArray()[jb_pass_index - 1]) {
//							jb_pass_flg = _MIX;
//							jd_chking = false;
//						}
//						if (dm_chking && dm_0 != ret_prd_infoList.get(i).getCus_sn().toCharArray()[dm_pass_index - 1]) {
//							dm_pass_flg = _MIX;
//							dm_chking = false;
//						}
//						if (!(jd_chking || dm_chking)) {
//							break;
//						}
//					}
//
//					if (!_MIX.equals(jb_pass_flg)) {
//						jb_pass_flg = (_YES_C == jb_0) ? _YES : _NO;
//					}
//					if (!_MIX.equals(dm_pass_flg)) {
//						dm_pass_flg = (_YES_C == dm_0) ? _YES : _NO;
//					}
//
//					outTrx.setJb_pass_flg(jb_pass_flg);
//					outTrx.setDm_pass_flg(dm_pass_flg);
//				}
//			}
		}
		getPrdMode(box.getBox_id());
		outTrx.setBox_id(box.getBox_id());
		outTrx.setLot_id_fk(box.getLot_id_fk());
		outTrx.setBox_stat(box.getBox_stat());
		outTrx.setValid_flg(box.getValid_flg());
		outTrx.setGroup_id(box.getGroup_id());
		outTrx.setPpbox_id(box.getPpbox_id());
		outTrx.setReal_emp(box.getReal_emp());
		outTrx.setBox_use_cnt(box.getBox_use_cnt());
		outTrx.setPrd_grade_fk(box.getPrd_grade_fk());
		outTrx.setStd_qty(box.getStd_qty());
		outTrx.setPrd_qty(box.getPrd_qty());
		outTrx.setMdl_typ_fk(box.getMdl_typ_fk());
		outTrx.setMdl_cate_fk(box.getMdl_cate_fk());
		outTrx.setMdl_id_fk(box.getMdl_id_fk());
		outTrx.setMdl_ec_code_fk(box.getMdl_ec_code_fk());
		outTrx.setMtrl_prod_id_fk(box.getMtrl_prod_id_fk());
		outTrx.setNx_path_id_fk(box.getNx_path_id_fk());
		outTrx.setNx_path_ver_fk(box.getNx_path_ver_fk());
		outTrx.setNx_ope_no_fk(box.getNx_ope_no_fk());
		outTrx.setNx_ope_id_fk(box.getNx_ope_id_fk());
		outTrx.setNx_ope_ver_fk(box.getNx_ope_ver_fk());
		outTrx.setNx_proc_id_fk(box.getNx_proc_id_fk());
		outTrx.setNx_toolg_id_fk(box.getNx_toolg_id_fk());
		outTrx.setNx_tool_id_fk(box.getNx_tool_id_fk());
		outTrx.setTool_id_fk(box.getTool_id_fk());
		outTrx.setTool_port_id_fk(box.getTool_port_id_fk());
		outTrx.setAct_cmp_timestamp(box.getAct_cmp_timestamp());
		outTrx.setWh_in_timestamp(box.getWh_in_timestamp());
		outTrx.setPrty(box.getPrty());
		outTrx.setPv_prty(box.getPv_prty());
		outTrx.setOqc_lotid(box.getOqc_lotid());
		outTrx.setOqc_skip_flg(box.getOqc_skip_flg());
		outTrx.setWo_id_fk(box.getWo_id_fk());
		outTrx.setDest_shop(box.getDest_shop());
		outTrx.setSo_id_fk(box.getSo_id_fk());
		outTrx.setPallet_id_fk(box.getPallet_id_fk());
		outTrx.setBatch_no_fk(box.getBatch_no_fk());
		outTrx.setRsv_no_fk(box.getRsv_no_fk());
		outTrx.setDn_no_fk(box.getDn_no_fk());
		outTrx.setCus_no(box.getCus_no());
		outTrx.setSn_no(box.getSn_no());
		outTrx.setShip_box_id(box.getShip_box_id());
		outTrx.setShipping_timestamp(box.getShipping_timestamp());
		outTrx.setBox_cate(box.getBox_cate());
		outTrx.setBox_size(box.getBox_size());
		outTrx.setBox_set_code(box.getBox_set_code());
		outTrx.setBox_mkr(box.getBox_mkr());
		outTrx.setBox_rgst_timestamp(box.getBox_rgst_timestamp());
		outTrx.setBox_own(box.getBox_own());
		outTrx.setBox_weight(box.getBox_weight());
		outTrx.setMdl_weight(box.getMdl_weight());
		outTrx.setMeg_box_id(box.getMeg_box_id());
		outTrx.setMeg_mdl_weight(box.getMeg_mdl_weight());
		outTrx.setRma_no(box.getRma_no());
		outTrx.setRma_timestamp(box.getRma_timestamp());
		outTrx.setBnk_flg(box.getBnk_flg());
		outTrx.setBox_def(box.getBox_def());
		outTrx.setDm_user(box.getExt_1());
		outTrx.setComp_date(HCutil.getDateTime(box.getEvt_timestamp().toString()));//完成时间，暂时以EVT_TIMESTAMP
		DateFormat evttime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		outTrx.setComp_date_all(evttime.format(box.getEvt_timestamp()));
		outTrx.setOverdue_date(Util.getDateAfter(box.getEvt_timestamp().toString(),6));//箱子失效时间为完成时间+6个月
		outTrx.setOverdue1(Util.getYesterData(outTrx.getOverdue_date()));
		if(outTrx.getJbpk_timestamp()!=null){
		   String overdate = Util.getDateAfter(outTrx.getJbpk_timestamp().toString(),6);
		   outTrx.setOverdate(Util.getYesterData(overdate));
		}
		outTrx.setEvt_user(box.getEvt_usr());
		outTrx.setBox_cnt(1);

		return _NORMAL;
	}

	private long summaryBoxInfoFor9100() {
		long rtnCode = 1;
		String boxId, ppboxId,outboxId,palletId;
		boxId = inTrx.getBox_id();
		ppboxId = inTrx.getPpbox_id();
		outboxId = inTrx.getShip_box_id();
		palletId = inTrx.getPallet_id();
		if (boxId != null && ppboxId != null) {
			gszMsgBuf = "不能同时按场内和内部箱号查询,请只按一种方式查询";
			return 1;
		}

		if (boxId != null) {
			rtnCode = sumBoxInfo(boxId);
		} else if (ppboxId != null) {
			rtnCode = sumPpboxInfo(ppboxId);
		} else if (outboxId != null){
			rtnCode = sumBoxInfo(outboxId);
		} else if (palletId != null){
			rtnCode = sumBoxInfobyPallet(palletId);
		}
		// 统计该站已完成数量
		return rtnCode;
	}
	
	private long sumBoxInfo(String boxId) {

		String woId, soId, prdStat, opeId, bnkFlg, lotId, opeVer, glot = "", oqc_skip_flg = " ",ext1="";

		Map<String, Ret_wo> woMap = new HashMap<String, Ret_wo>();
		Map<String, Ret_box> boxMap = new HashMap<String, Ret_box>();
		Map<StatBoxOpeBnkWoMtrlKey, String> lotMap = new HashMap<StatBoxOpeBnkWoMtrlKey, String>();
		Map<StatBoxOpeBnkWoMtrlKey, Integer> rowMap = new HashMap<StatBoxOpeBnkWoMtrlKey, Integer>();
		Map<WoOpeKey, Integer> unLogofMap = new HashMap<WoOpeKey, Integer>();
		Map<String, Integer> finishedMap = new HashMap<String, Integer>();
		Map<String, Integer> unFinishedMap = new HashMap<String, Integer>();

		List<XpinqboxO1> oary1List = new ArrayList<XpinqboxO1>();
		List<XpinqboxO2> oary2List = new ArrayList<XpinqboxO2>();
        //outbox
        String hqlbox = "FROM Ret_box WHERE ship_box_id=?";
		Ret_box retboxship = retBoxDao.uniqueResult(hqlbox, boxId);
        if(null != retboxship){
              boxId = retboxship.getBox_id();
        }
		
		String hql = "FROM Ret_prd_info where box_id_fk = '" + boxId + "'";
		if (inTrx.getWo_id() != null){
			hql += " and wo_id_fk = '" + inTrx.getWo_id() + "'"; 
		}

		List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.find(hql);
		for (Ret_prd_info retPrdInfo : retPrdInfoList) {
			boxId = retPrdInfo.getBox_id_fk();
			woId = retPrdInfo.getWo_id_fk();
			prdStat = retPrdInfo.getPrd_stat();
			opeId = "INPR".equals(prdStat) ? retPrdInfo.getCr_ope_id_fk() : retPrdInfo.getNx_ope_id_fk();
			String pathId = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_id_fk() : retPrdInfo.getCr_path_id_fk();
			String pathVer = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_ver_fk() : retPrdInfo.getCr_path_ver_fk();
			bnkFlg = retPrdInfo.getBnk_flg();
			lotId = retPrdInfo.getLot_id();
			// 统计在制数量,并确定行数
			StatBoxOpeBnkWoMtrlKey rowKey = new StatBoxOpeBnkWoMtrlKey(prdStat, opeId,
					boxId, bnkFlg, woId,retPrdInfo.getMtrl_box_id_fk());
			if (rowMap.containsKey(rowKey)) {
				rowMap.put(rowKey, rowMap.get(rowKey) + 1);
			} else {
				rowMap.put(rowKey, 1);
			}

			if (null != woId && !Util.isSpaceCheck(woId) && !woMap.containsKey(woId)) {
				Ret_wo retWo = retWoDao.get(woId);
				if (retWo != null) {
					woMap.put(woId, retWo);
				}
			}
			if (null != boxId && !Util.isSpaceCheck(boxId) && !boxMap.containsKey(boxId)) {
				Ret_box retBox = retBoxDao.get(boxId);
				if (null != retBox) {
					boxMap.put(boxId, retBox);
				}
			}

			if (null != lotId && !Util.isSpaceCheck(lotId)) {
				if (lotMap.containsKey(rowKey) && !lotId.equals(lotMap.get(rowKey))) {
					lotMap.put(rowKey, "MIX");
					glot = "MIX";
				} else {
					lotMap.put(rowKey, lotId);
					glot ="NOTMIX";
				}
			}
			XpinqboxO2 oary2 = new XpinqboxO2();
			oary2.setPrd_seq_id(retPrdInfo.getPrd_seq_id());
			oary2.setSlot_no(retPrdInfo.getSlot_no());
			if(null != retPrdInfo.getMtrl_slot_no_fk()){
				oary2.setMtrl_slot_no_fk(retPrdInfo.getMtrl_slot_no_fk());
			}
			oary2.setMdl_id(retPrdInfo.getMdl_id_fk());
			oary2.setOpe_id(opeId);
			oary2.setBnk_flg(bnkFlg);
			oary2.setPrd_stat(prdStat);
			oary2.setPrd_grade(retPrdInfo.getPrd_grade());
			oary2.setGroup_id(retPrdInfo.getGroup_id());
			//增加返工品标识
			Bis_pathiId id = new Bis_pathiId();
			id.setPath_id(pathId);
			id.setPath_ver(pathVer);
			Bis_pathi bis_pathi = bisPathiDao.get(id);
			if(bis_pathi !=null){
				if(bis_pathi.getPath_cate().equals(PATH_TYPE_REWK)){
					oary2.setOqc_skip_flg(_YES);
					oqc_skip_flg = "Y";
				}else{
					oary2.setOqc_skip_flg(_NO);
					oqc_skip_flg = "N";
				}
			}
			if(null!=retPrdInfo.getExt_1()){
				 oary2.setExt1(retPrdInfo.getExt_1());
				if(retPrdInfo.getExt_1().equals("N")){
				    ext1 = "N";
				}else{
					ext1 = "R";
				}
			}
			
//			if(!Util.isSpaceCheck(retPrdInfo.getOqc_skip_flg())){
//				oary2.setOqc_skip_flg(retPrdInfo.getOqc_skip_flg());
//				if (retPrdInfo.getOqc_skip_flg().equals("Y")) {
//					oqc_skip_flg = "Y";
//				}else if (retPrdInfo.getOqc_skip_flg().equals("N") && oqc_skip_flg == " " ){
//					oqc_skip_flg = "N";
//				}
//			}
			
			oary2.setMtrl_box_id(retPrdInfo.getMtrl_box_id_fk());
			oary2.setFab_sn(retPrdInfo.getFab_sn());
			oary2List.add(oary2);
		}
		Iterator<Entry<String, Ret_wo>> woIter = woMap.entrySet().iterator();
		while (woIter.hasNext()) {
			Entry<String, Ret_wo> entry = woIter.next();
			woId = entry.getKey();
			hql = "FROM Ret_prd_info where wo_id_fk = ? ";
			List<Ret_prd_info> retPrdInfoListByWo = retPrdInfoDao.list(hql,
					woId);
			for (Ret_prd_info retPrdInfo : retPrdInfoListByWo) {
				boxId = retPrdInfo.getBox_id_fk();
				prdStat = retPrdInfo.getPrd_stat();
				opeId = prdStat.equals("INPR") ? retPrdInfo.getCr_ope_id_fk()
						: retPrdInfo.getNx_ope_id_fk();
				bnkFlg = retPrdInfo.getBnk_flg();
				lotId = retPrdInfo.getLot_id();
				// 统计未出帐数量
				if ("INPR".equals(prdStat) || "WAIT".equals(prdStat)) {
					WoOpeKey unLogOfKey = new WoOpeKey(woId, opeId);
					if (unLogofMap.containsKey(unLogOfKey)) {
						unLogofMap.put(unLogOfKey,
								unLogofMap.get(unLogOfKey) + 1);
					} else {
						unLogofMap.put(unLogOfKey, 1);
					}
				}
				// 统计成品数量
				if ("SHIP".equals(prdStat) && "1".equals(bnkFlg)) {
					if (finishedMap.containsKey(woId)) {
						finishedMap.put(woId, finishedMap.get(woId) + 1);
					} else {
						finishedMap.put(woId, 1);
					}
				}
			}
			// 统计半成品数量
			if (!unFinishedMap.containsKey(woId)) {
				unFinishedMap.put(woId, fmtInt(woMap.get(woId).getRl_prd_qty())
						- fmtInt(finishedMap.get(woId)));
			}

		}
		Iterator<Entry<WoOpeKey, Integer>> testIt = unLogofMap.entrySet()
				.iterator();
		while (testIt.hasNext()) {
			Entry<WoOpeKey, Integer> testEntry = testIt.next();
			logger.info(testEntry.getKey());
			logger.info(testEntry.getValue());
		}
		// 统计该站已完成数量:列出所有的站点，去HIS中 count(*)

		Iterator<Entry<StatBoxOpeBnkWoMtrlKey, Integer>> rowIter = rowMap
				.entrySet().iterator();
		while (rowIter.hasNext()) {
			Entry<StatBoxOpeBnkWoMtrlKey, Integer> entry = rowIter.next();
			StatBoxOpeBnkWoMtrlKey rowKey = entry.getKey();
			logger.info(rowKey);
			boxId = rowKey.getBox_id();
			woId = rowKey.getWo_id();
			opeId = rowKey.getOpe_id();
			prdStat = rowKey.getPrd_stat();
			bnkFlg = rowKey.getBnk_flg();
			Ret_wo retWo = woMap.get(woId);
			if (null != retWo) {
				XpinqboxO1 oary1 = new XpinqboxO1();
				oary1.setCus_id(retWo.getCus_id_fk());
				soId = retWo.getSo_id();
				if (null != soId && !"".equals(soId)) {
					Ret_wo retSo = retWoDao.get(retWo.getSo_id());
					if (null != retSo) {
						oary1.setSo_id(retWo.getSo_id());
					}
				}
				Ret_box retBox = retBoxDao.get(boxId);
				String hqldest="";
				if(!Util.isSpaceCheck(retBox.getDest_shop())){
					if(!Util.isSpaceCheck(retBox.getBox_def())){
						 hqldest = "FROM Bis_data WHERE data_cate = 'DEST' AND data_id = '" + retBox.getBox_def() + "'";
					}else{
				         hqldest = "FROM Bis_data WHERE data_cate = 'DEST' AND data_id = '" + retBox.getDest_shop() + "'"; 
					}
				List<Bis_data> dataList = bisDataDao.find(hqldest);
				if(dataList.size()>0){
				   oary1.setDes_data_desc(dataList.get(0).getData_desc());
				}
				}
				if(!Util.isSpaceCheck(retBox.getShip_box_id())){
					oary1.setShip_box_id(retBox.getShip_box_id());
				}
				if(!Util.isSpaceCheck(retBox.getPallet_id_fk())){
					oary1.setPallet_id(retBox.getPallet_id_fk());
				}
				oary1.setWo_id(woId);
				oary1.setMtrl_box_id_fk(rowKey.getMtrl_box_id());
				oary1.setMdl_id(retWo.getMdl_id_fk());
				oary1.setBox_stat((boxId != null && !"".equals(boxId)) ? retBox
						.getBox_stat() : "");
				if(glot.equals("MIX")){
					oary1.setLot_id(glot);
				}else{
					oary1.setLot_id(lotMap.get(rowKey));
				}
				oary1.setBox_id(boxId);
				oary1.setPrd_qty(null == retBox ? null : retBox.getPrd_qty());
				oary1.setWip_qty(null == retBox ? null : retBox.getPrd_qty());
				// oary1.setWip_qty(fmtInt(entry.getValue()));
				oary1.setFinished_qty(fmtInt(finishedMap.get(woId)));
				oary1.setUn_finished_qty(fmtInt(unFinishedMap.get(woId)));
				oary1.setPrd_stat(prdStat);
				oary1.setOpe_id(opeId);
				oary1.setBnk_flg(bnkFlg);
				oary1.setUn_logof_qty(fmtInt(unLogofMap.get(new WoOpeKey(woId,
						opeId))));
				oary1.setOqc_skip_flg(oqc_skip_flg);
				oary1.setExt1(ext1);
				logger.info(oary1.getUn_logof_qty());
				// oary1.setLogof_qty(logof_qty) TODO:等待HIS_RET_PRD_INFO表完成后再完善
				oary1List.add(oary1);
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary1(oary1List);
		outTrx.setOary1_cnt(oary1List.size());
		outTrx.setOary2(oary2List);
		outTrx.setOary2_cnt(oary2List.size());
		return 0;
	}

	private long sumPpboxInfo(String ppboxId) {

		String woId, soId, prdStat, opeId, bnkFlg, lotId, boxId,oqc_skip_flg = " ",ext1=" ";
		String[] rtflg;

		StringBuffer shtHqlBuf = new StringBuffer();

		Map<String, Ret_wo> woMap = new HashMap<String, Ret_wo>();
		Map<String, Ret_box> boxMap = new HashMap<String, Ret_box>();
		Map<StatBoxOpeBnkWoKey, String> lotMap = new HashMap<StatBoxOpeBnkWoKey, String>();
		Map<StatBoxOpeBnkWoKey, String> oqcskpflgMap = new HashMap<StatBoxOpeBnkWoKey, String>();
		Map<StatBoxOpeBnkWoKey, String> rmaflgMap = new HashMap<StatBoxOpeBnkWoKey, String>();
		Map<StatBoxOpeBnkWoKey, Integer> rowMap = new HashMap<StatBoxOpeBnkWoKey, Integer>();
		Map<WoOpeKey, Integer> unLogofMap = new HashMap<WoOpeKey, Integer>();
		Map<String, Integer> finishedMap = new HashMap<String, Integer>();
		Map<String, Integer> unFinishedMap = new HashMap<String, Integer>();

		List<XpinqboxO1> oary1List = new ArrayList<XpinqboxO1>();
		List<XpinqboxO2> oary2List = new ArrayList<XpinqboxO2>();

		String hql = "FROM Ret_prd_in where mtrl_box_id = '" + ppboxId + "'";
		if (inTrx.getWo_id() != null){
			hql += " and wo_id_fk = '" + inTrx.getWo_id() + "'"; 
		}
		Ret_prd_in retPrdIn = (Ret_prd_in) retPrdInDao.getCrSession().createQuery(hql).uniqueResult();
		if (retPrdIn == null) {
			gszMsgBuf = "找不到来料箱记录";
			return 1;
		}

		hql = "FROM Ret_prd_indt where mtrl_box_id_fk = '" + ppboxId + "'";
		List<Ret_prd_indt> retPrdIndtList = retPrdIndtDao.find(hql);
		if (retPrdIndtList.size() <= 0) {
			gszMsgBuf = "找不到来料箱中的产品记录";
			return 2;
		}
		if (inTrx.getWo_id() != null){
			shtHqlBuf.append("FROM Ret_prd_info where wo_id_fk = '" + inTrx.getWo_id() + "' and prd_seq_id IN (");
		}else{
			shtHqlBuf.append("FROM Ret_prd_info where prd_seq_id IN (");
		}
		for (Ret_prd_indt retPrdIndt : retPrdIndtList) {
			shtHqlBuf.append("'")
					.append(retPrdIndt.getId().getVdr_prd_seq_id())
					.append("',");
		}
		shtHqlBuf.deleteCharAt(shtHqlBuf.length() - 1);
		shtHqlBuf.append(") order by box_id_fk");

		List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.find(shtHqlBuf.toString());
		for (Ret_prd_info retPrdInfo : retPrdInfoList) {
			boxId = retPrdInfo.getBox_id_fk();
			woId = retPrdInfo.getWo_id_fk();
			prdStat = retPrdInfo.getPrd_stat();
			if(prdStat.equals("INPR")||prdStat.equals("RMWI")||prdStat.equals("RMPI")){
				opeId =retPrdInfo.getCr_ope_id_fk();
			}else{
				opeId = retPrdInfo.getNx_ope_id_fk();
			}
//			opeId = prdStat.equals("INPR") ? retPrdInfo.getCr_ope_id_fk()
//					: retPrdInfo.getNx_ope_id_fk();
			String pathId = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_id_fk() : retPrdInfo.getCr_path_id_fk();
			String pathVer = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_ver_fk() : retPrdInfo.getCr_path_ver_fk();
			bnkFlg = retPrdInfo.getBnk_flg();
			lotId = retPrdInfo.getLot_id();
			// 统计在制数量,并确定行数
			StatBoxOpeBnkWoKey rowKey = new StatBoxOpeBnkWoKey(prdStat, opeId,
					boxId, bnkFlg, woId);
			if (rowMap.containsKey(rowKey)) {
				rowMap.put(rowKey, rowMap.get(rowKey) + 1);
			} else {
				rowMap.put(rowKey, 1);
			}
			
//			if(!Util.isSpaceCheck(retPrdInfo.getOqc_skip_flg())){
//				oqc_skip_flg = retPrdInfo.getOqc_skip_flg();
//			}
			//增加返工品标识
			Bis_pathiId id = new Bis_pathiId();
			id.setPath_id(pathId);
			id.setPath_ver(pathVer);
			Bis_pathi bis_pathi = bisPathiDao.get(id);
			if(bis_pathi !=null){
				if(bis_pathi.getPath_cate().equals(PATH_TYPE_REWK)){
					oqc_skip_flg = "Y";
				}else{
					oqc_skip_flg = "N";
				}
			}
			if(null!=retPrdInfo.getExt_1()){
				if(retPrdInfo.getExt_1().equals("R")){
				    ext1 = "R";
				}else{
					ext1 = "N";
				}
			}else{
				ext1 ="N";
			}
			if (oqcskpflgMap.containsKey(rowKey)) {
				if ( oqc_skip_flg.equals("Y") ) {
					oqcskpflgMap.put(rowKey, oqc_skip_flg);
				}else if (oqc_skip_flg.equals("N") && oqcskpflgMap.get(rowKey).equals(" ")) {
					oqcskpflgMap.put(rowKey, oqc_skip_flg);
				};
			} else {
				oqcskpflgMap.put(rowKey, oqc_skip_flg);
			}
		    rmaflgMap.put(rowKey, ext1);
			if (!woMap.containsKey(woId)) {
				logger.info("woId:" + woId);
				Ret_wo retWo = retWoDao.get(woId);
				if (retWo != null) {
					woMap.put(woId, retWo);
				}
			}
			if (null != boxId && !"".equals(boxId)
					&& !boxMap.containsKey(boxId)) {
				logger.info("boxId :" + boxId);
				Ret_box retBox = retBoxDao.get(boxId);
				if (null != retBox) {
					boxMap.put(boxId, retBox);
				}
			}
			if (lotId != null) {
				if (lotMap.containsKey(rowKey)
						&& !lotId.equals(lotMap.get(rowKey))) {
					lotMap.put(rowKey, "MIX");
				} else {
					lotMap.put(rowKey, lotId);
				}
			}
			XpinqboxO2 oary2 = new XpinqboxO2();
			oary2.setPrd_seq_id(retPrdInfo.getPrd_seq_id());
			oary2.setSlot_no(retPrdInfo.getSlot_no());
			if(null != retPrdInfo.getMtrl_slot_no_fk()){
				oary2.setMtrl_slot_no_fk(retPrdInfo.getMtrl_slot_no_fk());
			}
			oary2.setMdl_id(retPrdInfo.getMdl_id_fk());
			oary2.setOpe_id(opeId);
			oary2.setBnk_flg(bnkFlg);
			oary2.setPrd_stat(prdStat);
			oary2.setPrd_grade(retPrdInfo.getPrd_grade());
			oary2.setGroup_id(retPrdInfo.getGroup_id());
			oary2.setExt1(ext1);
			//增加返工品标识
//			if(!Util.isSpaceCheck(retPrdInfo.getOqc_skip_flg())){
//				oary2.setOqc_skip_flg(retPrdInfo.getOqc_skip_flg());
//			}
			oary2.setOqc_skip_flg(oqc_skip_flg);
			oary2.setMtrl_box_id(retPrdInfo.getMtrl_box_id_fk());
			oary2.setFab_sn(retPrdInfo.getFab_sn());
			oary2List.add(oary2);

		}
		Iterator<Entry<String, Ret_wo>> woIter = woMap.entrySet().iterator();
		while (woIter.hasNext()) {
			Entry<String, Ret_wo> entry = woIter.next();
			woId = entry.getKey();
			hql = "FROM Ret_prd_info where wo_id_fk ='" + woId + "'";
			List<Ret_prd_info> retPrdInfoListByWo = retPrdInfoDao.find(hql);
			for (Ret_prd_info retPrdInfo : retPrdInfoListByWo) {
				boxId = retPrdInfo.getBox_id_fk();
				prdStat = retPrdInfo.getPrd_stat();
				opeId = prdStat.equals("INPR") ? retPrdInfo.getCr_ope_id_fk()
						: retPrdInfo.getNx_ope_id_fk();
				bnkFlg = retPrdInfo.getBnk_flg();
				lotId = retPrdInfo.getLot_id();
				// 统计未出帐数量
				if ("INPR".equals(prdStat) || "WAIT".equals(prdStat)) {
					WoOpeKey unLogOfKey = new WoOpeKey(woId, opeId);
					if (unLogofMap.containsKey(unLogOfKey)) {
						unLogofMap.put(unLogOfKey,
								unLogofMap.get(unLogOfKey) + 1);
					} else {
						unLogofMap.put(unLogOfKey, 1);
					}
				}
			}

		}
		// 统计该站已完成数量:列出所有的站点，去HIS中 count(*)

		Iterator<Entry<StatBoxOpeBnkWoKey, Integer>> rowIter = rowMap
				.entrySet().iterator();
		while (rowIter.hasNext()) {
			Entry<StatBoxOpeBnkWoKey, Integer> entry = rowIter.next();
			StatBoxOpeBnkWoKey rowKey = entry.getKey();
			boxId = rowKey.getBox_id();
			woId = rowKey.getWo_id();
			opeId = rowKey.getOpe_id();
			prdStat = rowKey.getPrd_stat();
			bnkFlg = rowKey.getBnk_flg();
			Ret_wo retWo = woMap.get(woId);
			//List<Ret_prd_info> prdInfoList1 = new ArrayList<Ret_prd_info>();
			
			
			if (null != retWo) {
				XpinqboxO1 oary1 = new XpinqboxO1();
				oary1.setCus_id(retWo.getCus_id_fk());
				soId = retWo.getSo_id();
				if (null != soId && !"".equals(soId)) {
					Ret_wo retSo = retWoDao.get(retWo.getSo_id());
					if (null != retSo) {
						oary1.setSo_id(retWo.getSo_id());
					}
				}
				oary1.setWo_id(woId);
				oary1.setMdl_id(retWo.getMdl_id_fk());
				oary1.setBox_stat(prdStat);
				oary1.setLot_id(lotMap.get(rowKey));
				oary1.setBox_id(boxId);
				if(!Util.isSpaceCheck(boxId)){
				Ret_box retBox = retBoxDao.get(boxId);
				String hqldest="";
				if(!Util.isSpaceCheck(retBox.getDest_shop())){
					if(!Util.isSpaceCheck(retBox.getBox_def())){
						 hqldest = "FROM Bis_data WHERE data_cate = 'DEST' AND data_id = '" + retBox.getBox_def() + "'";
					}else{
				         hqldest = "FROM Bis_data WHERE data_cate = 'DEST' AND data_id = '" + retBox.getDest_shop() + "'"; 
					} 
				List<Bis_data> dataList = bisDataDao.find(hqldest);
				if(dataList.size()>0){
				oary1.setDes_data_desc(dataList.get(0).getData_desc());
				}
				}
				if(!Util.isSpaceCheck(retBox.getShip_box_id())){
					oary1.setShip_box_id(retBox.getShip_box_id());
				}
				if(!Util.isSpaceCheck(retBox.getPallet_id_fk())){
					oary1.setPallet_id(retBox.getPallet_id_fk());
				}
				}
				oary1.setPrd_qty(retPrdIn.getPrd_tl_qty());
				oary1.setWip_qty(fmtInt(entry.getValue()));
				// 统计成品数量
				if ("SHIP".equals(prdStat) && "1".equals(bnkFlg)) {
					if (finishedMap.containsKey(woId)) {
						finishedMap.put(woId, finishedMap.get(woId));
					} else {
						finishedMap.put(woId, 1);
					}
				}
				// 统计成品数量
				oary1.setFinished_qty(fmtInt(woMap.get(woId).getWh_in_prd_qty())+fmtInt(woMap.get(woId).getComp_prd_qty()));
//				if (!unFinishedMap.containsKey(woId)) {
//					unFinishedMap.put(woId, fmtInt(woMap.get(woId)
//							.getRl_prd_qty())
//							- fmtInt(woMap.get(woId).getWh_in_prd_qty()));
//				}
				oary1.setUn_finished_qty(fmtInt(woMap.get(woId).getPln_prd_qty())-fmtInt(woMap.get(woId).getComp_prd_qty())-fmtInt(woMap.get(woId).getWh_in_prd_qty())-fmtInt(woMap.get(woId).getWh_out_prd_qty())-fmtInt(woMap.get(woId).getWip_wh_in_prd_qty()));
				oary1.setPrd_stat(retPrdIn.getPpbox_stat());
				oary1.setOpe_id(opeId);
				oary1.setBnk_flg(bnkFlg);
				oary1.setUn_logof_qty(fmtInt(unLogofMap.get(new WoOpeKey(woId,
						opeId))));
				oary1.setMtrl_box_id_fk(ppboxId);
				oary1.setOqc_skip_flg(oqcskpflgMap.get(rowKey));
				oary1.setExt1(rmaflgMap.get(rowKey));
				oary1List.add(oary1);
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary1(oary1List);
		outTrx.setOary1_cnt(oary1List.size());
		outTrx.setOary2(oary2List);
		outTrx.setOary2_cnt(oary2List.size());
		return 0;
	}

	private long queryRetainInfo() {
		List<Ret_box> boxListOary = new ArrayList<Ret_box>();
		// 查询在制箱号
		String sql = "FROM Ret_box where box_stat ='EMPT' AND real_emp='N' AND bnk_flg='3'";
		if(!Util.isSpaceCheck(inTrx.getWo_id())){
			sql += " AND wo_id_fk='" + inTrx.getWo_id() + "'";
		}
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		boxList = retBoxDao.find(sql);
		for (int i = 0; i < boxList.size(); i++) {
			// 获取在制箱号中的产品信息
			List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
			prdInfoList = retPrdInfoDao.getShtListInfo(boxList.get(i).getBox_id());
			if (prdInfoList!=null) {
				if (prdInfoList.get(0).getCr_ope_id_fk().equals(inTrx.getOpe_id())
						&& prdInfoList.get(0).getCr_ope_ver_fk().equals(inTrx.getOpe_ver())) {
					boxListOary.add(boxList.get(i));
				}
			}
		}
		outTrx.setBox_cnt(boxListOary.size());
		outTrx.setOary_box(boxListOary);
		return _NORMAL;
	}

	private long queryBoxInfo() {
		long lRc;
		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			gszMsgBuf = "Input box id [" + inTrx.getBox_id() + "] is null!";
			return E_XPINQBOX_INVALID_INPUT_BOX_ID;
		}

		Ret_box box = new Ret_box();
		box = retBoxDao.get(inTrx.getBox_id());
		if (null != box) {
			outTrx.setBox_id(box.getBox_id());
			outTrx.setLot_id_fk(box.getLot_id_fk());
			outTrx.setBox_stat(box.getBox_stat());
			outTrx.setValid_flg(box.getValid_flg());
			outTrx.setReal_emp(box.getReal_emp());
			outTrx.setBox_use_cnt(box.getBox_use_cnt());
			outTrx.setPrd_grade_fk(box.getPrd_grade_fk());
			outTrx.setStd_qty(box.getStd_qty());
			outTrx.setPrd_qty(box.getPrd_qty());
			outTrx.setWo_id_fk(box.getWo_id_fk());
			outTrx.setBnk_flg(box.getBnk_flg());
			// Query prd list in box and set to oary
			lRc = setOutTrxByPpboxInfo(inTrx.getBox_id());
			if (_NORMAL != lRc) {
				return lRc;
			}
		}
		return _NORMAL;
	}

	/**
	 * Query prd list in box and set to oary
	 */
	private long setOutTrxByPpboxInfo(String boxID) {
		long lRc = _NORMAL;
		Integer no_lot_prd_qty = 0;
		Boolean isMixLot = false;// 标记是否混批次true:混批次，false:不混批次
		Boolean isNullLot = true;// 标记箱子中产品是否均有批次号true:有空批次，false:无空批次
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		List<XpinqboxO3> oary3List = new ArrayList<XpinqboxO3>();
		List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
		ret_prd_infoList = retPrdInfoDao.getShtListInfo(boxID);
		if (null != ret_prd_infoList) {
			if ( !ret_prd_infoList.isEmpty() ){
				outTrx.setOary(ret_prd_infoList);
				
			}

			
			/******************** 判断箱子中未绑定批次的产品数量 ******************/
			for (int i = 0; i < ret_prd_infoList.size(); i++) {
				if (Util.isSpaceCheck(ret_prd_infoList.get(i).getLot_id())) {
					no_lot_prd_qty++;
				}
			}

			if (no_lot_prd_qty == 0) {// 无空批次
				isNullLot = false;
				outTrx.setCr_lot_flg(_NO);
			} else { // 有空批次
				isNullLot = true;
				outTrx.setCr_lot_flg(_YES);
			}

			if (!isNullLot) {// 箱子中产品均有批次，判断是否混批次
				for (int j = 1; j < ret_prd_infoList.size(); j++) {
					if (!ret_prd_infoList.get(0).getLot_id()
							.equals(ret_prd_infoList.get(j).getLot_id())) {
						isMixLot = true;
						break;
					}
				}
			}

			if (isMixLot) {
				outTrx.setLot_id_fk(LOT_MIX);
			} else if (isNullLot && no_lot_prd_qty != ret_prd_infoList.size()) {
				outTrx.setLot_id_fk(LOT_MIX);
			} else {
				outTrx.setLot_id_fk(ret_prd_infoList.get(0).getLot_id());
			}

			outTrx.setNo_lot_prd_qty(no_lot_prd_qty);
			/******************** 获取查询到的第一条记录，放入oary3List中 ******************/
			XpinqboxO3 oary3_1 = new XpinqboxO3();
			Ret_prd_in ret_prd_in = new Ret_prd_in();
			rtnBeen = retPrdInDao
					.getRetPrdInByWoAndBox(ret_prd_infoList.get(0)
							.getWo_id_fk(), ret_prd_infoList.get(0)
							.getMtrl_box_id_fk());
			if (E_RET_PRD_IN + E_READ_NOT_FOUND == rtnBeen.getlRc()) {
				gszMsgBuf = "内部订单[" + ret_prd_infoList.get(0).getWo_id_fk()
						+ "]中没有箱子["
						+ ret_prd_infoList.get(0).getMtrl_box_id_fk() + "]!";
				return rtnBeen.getlRc();
			} else {
				ret_prd_in = (Ret_prd_in) rtnBeen.getObj();
			}
			oary3_1.setMtrl_box_id(ret_prd_infoList.get(0).getMtrl_box_id_fk());
			oary3_1.setPrd_tl_qty(ret_prd_in.getPrd_tl_qty());
			oary3_1.setPrd_cat_qty(ret_prd_in.getPrd_act_qty());
			if (ret_prd_in.getPrd_act_qty() != 0) {
				oary3_1.setW_flg(_YES);
			} else {
				oary3_1.setW_flg(_NO);
			}
			oary3List.add(oary3_1);
			/*************************** 筛选来料箱信息 *****************************/
			for (int j = 1; j < ret_prd_infoList.size(); j++) {
				for (int k = 0; k < oary3List.size(); k++) {
					if (ret_prd_infoList.get(j).getMtrl_box_id_fk()
							.equals(oary3List.get(k).getMtrl_box_id())) {
						break;
					}
					if (k == oary3List.size() - 1) {
						XpinqboxO3 oary3_2 = new XpinqboxO3();
						Ret_prd_in ret_prd_in_2 = new Ret_prd_in();
						rtnBeen = retPrdInDao.getRetPrdInByWoAndBox(
								ret_prd_infoList.get(j).getWo_id_fk(),
								ret_prd_infoList.get(j).getMtrl_box_id_fk());
						if (E_RET_PRD_IN + E_READ_NOT_FOUND == rtnBeen.getlRc()) {
							gszMsgBuf = "内部订单["
									+ ret_prd_infoList.get(j).getWo_id_fk()
									+ "]中没有箱子["
									+ ret_prd_infoList.get(j)
											.getMtrl_box_id_fk() + "]!";
							return rtnBeen.getlRc();
						} else {
							ret_prd_in_2 = (Ret_prd_in) rtnBeen.getObj();
						}
						oary3_2.setMtrl_box_id(ret_prd_infoList.get(j)
								.getMtrl_box_id_fk());
						oary3_2.setPrd_tl_qty(ret_prd_in_2.getPrd_tl_qty());
						oary3_2.setPrd_cat_qty(ret_prd_in_2.getPrd_act_qty());
						if (ret_prd_in_2.getPrd_act_qty() != 0) {
							oary3_2.setW_flg(_YES);
						} else {
							oary3_2.setW_flg(_NO);
						}
						oary3List.add(oary3_2);
					}
				}
			}
			outTrx.setOary3(oary3List);
			outTrx.setOary3_cnt(oary3List.size());
		}
		return lRc;
	}
	
	private long changePrdAdminFlg(){
		List<XpinqboxIA> iary = inTrx.getIary();
        String admin_flg = "";
		for (int i=0;i<iary.size();i++){
			String box_id = iary.get(i).getBox_id();
			if (Util.isSpaceCheck(box_id)) {
				gszMsgBuf = "Input box id [" + box_id + "] is null!";
				return E_XPINQBOX_INVALID_INPUT_BOX_ID;
			}
			String prd_admin_flg = iary.get(i).getPrd_admin_flg();
			if (Util.isSpaceCheck(box_id)) {
				gszMsgBuf = "Input prd_admin_flg [" + prd_admin_flg + "] is null!";
				return E_XPINQBOX_INVALID_INPUT_BOX_ID;
			}
			char prd_admin_flg_change;
			if (_YES.equals(prd_admin_flg)){
				prd_admin_flg_change = 'N';
			}else if(_NO.equals(prd_admin_flg)){
				prd_admin_flg_change = 'Y';
			}else {
				prd_admin_flg_change = 'N';
			}
			Ret_box ret_box = retBoxDao.get(box_id);
			if (null != ret_box.getPrd_admin_flg()){
				StringBuffer sb = new StringBuffer(ret_box.getPrd_admin_flg());
				sb.setCharAt(0,prd_admin_flg_change);
//				update Ret_box
				ret_box.setPrd_admin_flg(sb.toString());
				admin_flg = sb.toString();
			}else{
				ret_box.setPrd_admin_flg(prd_admin_flg_change + "NNNNNNNNNNNNNNNNNNN");
				admin_flg = ret_box.getPrd_admin_flg();
			}
			retBoxDao.update(ret_box);
			
//			update Ret_prd_info
			String sql = "FROM Ret_prd_info where box_id_fk = '" + box_id + "'";
			List<Ret_prd_info> ret_prd_info_list = new ArrayList();
			ret_prd_info_list = retPrdInfoDao.find(sql);
			for (Ret_prd_info prd : ret_prd_info_list){
				if(null != prd.getPrd_admin_flg()){
					prd.setPrd_admin_flg(prd_admin_flg_change + prd.getPrd_admin_flg().substring(1));
				}else{
					prd.setPrd_admin_flg(admin_flg);					
				}
				retPrdInfoDao.update(prd);
			}
		}
		return _NORMAL;
	}
	
	private void getPrdMode(String boxId) {
		Integer mode_cnt = 0;
		Integer smode_cnt = 0;
		Integer okCnt = 0;
		Integer gkCnt = 0;
		Integer blCnt = 0;
		Integer sdefCnt = 0;
		String oky = "";
		List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
		// Get prd info
		ret_prd_infoList = retPrdInfoDao.getShtListInfo(boxId);
		if(ret_prd_infoList != null){
			mode_cnt = ret_prd_infoList.size() * ret_prd_infoList.get(0).getX_axis_cnt_fk() * ret_prd_infoList.get(0).getY_axis_cnt_fk();
			smode_cnt = ret_prd_infoList.get(0).getX_axis_cnt_fk() * ret_prd_infoList.get(0).getY_axis_cnt_fk();
			outTrx.setMtrl_box_id_fk(ret_prd_infoList.get(0).getMtrl_box_id_fk());
			if(ret_prd_infoList.get(0).getP3_timestamp()!=null){
			DateFormat jbtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			outTrx.setJbpk_timestamp(jbtime.format(ret_prd_infoList.get(0).getP3_timestamp()));
			}
			//String jbpk_time= (String)(ret_prd_infoList.get(0).getP3_timestamp());
			for(Ret_prd_info prd:ret_prd_infoList){
				String defsql = "SELECT PRD_SEQ_ID_FK,SUM(DEF_CODE1_CNT) DEF_CNT FROM RET_PRD_JGE WHERE PRD_SEQ_ID_FK='" + 
			                    prd.getPrd_seq_id() + "'";
				List<Object[]> objlist = retPrdJgeDao.findBySQL(defsql);
			       Object cntinfo[] = objlist.get(0);
			    if(null != cntinfo[1]){
			       String sCnt = cntinfo[1].toString();
			       sdefCnt = Integer.valueOf(sCnt);
			       blCnt = blCnt + sdefCnt;
			     }
				if(prd.getPrd_grade().equals("OK")){
					okCnt++;
				}else if(prd.getPrd_grade().equals("GK")){
					gkCnt++;
				}
			}
			if(okCnt>0){
				outTrx.setGrd098("良品");
			}else if(gkCnt.equals(ret_prd_infoList.size())){
				outTrx.setGrd098("管控品");
			}else{
				outTrx.setGrd098("滞留品");
			}
			NumberFormat format = new DecimalFormat("0.0");
			Double db = new Double((double)(mode_cnt-blCnt)/(double)mode_cnt*100);
		    oky = format.format(db);
		}
		outTrx.setOkyield(oky);
		outTrx.setBl_cnt(blCnt);
		outTrx.setMode_cnt(mode_cnt);
		outTrx.setSmode_cnt(smode_cnt);
	}
	
	private long getPrdList(){
		List<Ret_prd_info> prd_list = new ArrayList<Ret_prd_info>();
		String hql = "FROM Ret_prd_info where box_id_fk = '" +inTrx.getBox_id()+"'";
		prd_list = retPrdInfoDao.find(hql);
		if(prd_list.size() > 0){
			if (prd_list.get(0).getPrd_grade().equals("SC")){
				outTrx.setValid_flg("SC");
			}else {
				outTrx.setValid_flg("OKNG");
			}
		}
		return _NORMAL;
	}
	
	private long getPrdInfoList(){
		List<Ret_prd_info> prd_list = new ArrayList<Ret_prd_info>();
		String hql = "FROM Ret_prd_info WHERE prd_stat = 'INPR' AND box_id_fk = '' AND bnk_flg = '3' AND cr_ope_id_fk = '" + inTrx.getOpe_id()+"'"+
					 "AND prd_grade = 'SC' AND cr_ope_ver_fk = '" + inTrx.getOpe_ver() + "'";
		prd_list = retPrdInfoDao.find(hql);
		if (!prd_list.isEmpty()){
			outTrx.setOary(prd_list);
		}
		return _NORMAL;
	}
	
	private Integer fmtInt(Integer val) {
		return null == val ? 0 : val;
	}
	
	private long getLwInfo(){
		Integer lw_cnt=0;
		List<Ret_prd_info> prd_list = new ArrayList<Ret_prd_info>();
		String hql = "FROM Ret_prd_info where box_id_fk = '" +inTrx.getBox_id()+"'";
		prd_list = retPrdInfoDao.find(hql);
		if(prd_list.size() > 0){
          for(Ret_prd_info prd:prd_list){
			List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();
			String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '" + prd.getPrd_seq_id() + "'  AND proc_id ='DMQA'";
			ret_prd_jgeList = retPrdJgeDao.find(sql);
			for(Ret_prd_jge jde:ret_prd_jgeList){
				if("Z005".equals(jde.getDef_code1())||"L005".equals(jde.getDef_code1())||"L016".equals(jde.getDef_code1())||"Z016".equals(jde.getDef_code1())||"Z036".equals(jde.getDef_code1())){
					lw_cnt+=jde.getDef_code1_cnt();
				}
				if("Z005".equals(jde.getDef_code2())||"L005".equals(jde.getDef_code2())||"L016".equals(jde.getDef_code2())||"Z016".equals(jde.getDef_code2())||"Z036".equals(jde.getDef_code2())){
					lw_cnt+=jde.getDef_code2_cnt();
				}
				if("Z005".equals(jde.getDef_code3())||"L005".equals(jde.getDef_code3())||"L016".equals(jde.getDef_code3())||"Z016".equals(jde.getDef_code3())||"Z036".equals(jde.getDef_code3())){
					lw_cnt+=jde.getDef_code3_cnt();
				}
				if("Z005".equals(jde.getDef_code4())||"L005".equals(jde.getDef_code4())||"L016".equals(jde.getDef_code4())||"Z016".equals(jde.getDef_code4())||"Z036".equals(jde.getDef_code4())){
					lw_cnt+=jde.getDef_code4_cnt();
				}
				if("Z005".equals(jde.getDef_code5())||"L005".equals(jde.getDef_code5())||"L016".equals(jde.getDef_code5())||"Z016".equals(jde.getDef_code5())||"Z036".equals(jde.getDef_code5())){
					lw_cnt+=jde.getDef_code5_cnt();
				}
			}
          }
		}
		outTrx.setLw_cnt(lw_cnt);
		return _NORMAL;
	}
	
	/**
	 * 4C00补印出货箱信息查询
	 * 
	 * @return
	 */
	private long getBoxInfofor4C00() {
		long lRc = _NORMAL;
		String sql = "FROM Ret_box WHERE 1=1 ";

		if (!Util.isSpaceCheck(inTrx.getBox_id())) {
			sql += " AND box_id='" + inTrx.getBox_id() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getWo_id())) {
			sql += " AND wo_id_fk = '" + inTrx.getWo_id() + "' ";
		}
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		List<XpinqboxO4> oaryList = new ArrayList<XpinqboxO4>();
		boxList = retBoxDao.find(sql);
		if(boxList.size()>0){
			for(Ret_box boxinfo:boxList){
				XpinqboxO4 oary4 = new XpinqboxO4();
				oary4.setBox_id(boxinfo.getBox_id());
				oary4.setSo_id_fk(boxinfo.getSo_id_fk());
				oary4.setComp_date(Util.getDateTime(Util.getCurrentTimestamp().toString()).toString());
				oary4.setOverdue_date(Util.getDateAfter(boxinfo.getEvt_timestamp().toString(),6));
				oary4.setOverdue1(Util.getYesterData(oary4.getOverdue_date()));
				oary4.setPrd_qty(boxinfo.getPrd_qty());
				oary4.setPrd_grade_fk(boxinfo.getPrd_grade_fk());
				oary4.setDm_user(boxinfo.getEvt_usr());
				oary4.setWo_id(boxinfo.getWo_id_fk());
				Ret_wo ret_wo = new Ret_wo();
				ret_wo = retWoDao.get(boxinfo.getWo_id_fk());
				if(ret_wo != null){
					oary4.setCus_id_fk(ret_wo.getCus_id_fk());
					oary4.setTo_thickness(ret_wo.getTo_thickness());
					oary4.setCus_info_fst(ret_wo.getCus_info_fst());
					oary4.setCus_info_snd(ret_wo.getCus_info_snd());
					oary4.setFm_mdl_id_fk(ret_wo.getFm_mdl_id_fk());
				}
				oaryList.add(oary4);
			}
		}
		outTrx.setBox_cnt(0);
		if (null == boxList) {
			outTrx.setBox_cnt(0);
		} else {
			outTrx.setOary4_cnt(boxList.size());
			outTrx.setOary4(oaryList);
		}

		return lRc;
	}
	/**
	 * 查询箱子中产品的重工次数
	 */
	private long queryPrdRwCnt(){
		long lRc = _NORMAL;
		int index = 0;
		Integer rwk_prd_cnt = 0;
		List<XpinqboxO5> oaryList = new ArrayList<XpinqboxO5>();
		List<Ret_prd_info> prd_list = new ArrayList<Ret_prd_info>();
		String hql = "FROM Ret_prd_info where box_id_fk = '" +inTrx.getBox_id()+"'";
		prd_list = retPrdInfoDao.find(hql);
		if(prd_list.size()>0){
			String cr_ope_id = prd_list.get(0).getCr_ope_id_fk();
			String cr_ope_ver = prd_list.get(0).getCr_ope_ver_fk();
			
			Bis_opeId bis_opeId = new Bis_opeId();
			Bis_ope bis_ope = new Bis_ope();
			
			bis_opeId.setOpe_id(cr_ope_id);
			bis_opeId.setOpe_ver(cr_ope_ver);
			
			bis_ope = bisOpeDao.get(bis_opeId);
			String pep_lvl = bis_ope.getPep_lvl();
			
			index = Integer.valueOf(pep_lvl) - 1;
			outTrx.setBox_id(prd_list.get(0).getBox_id_fk());
			outTrx.setBox_stat(prd_list.get(0).getPrd_stat());
		}
		for(int i=0; i<prd_list.size(); i++){
			XpinqboxO5 oary5 = new XpinqboxO5();
			StringBuffer cus_sn = new StringBuffer(prd_list.get(i).getCus_sn());
			int count = 0 ;
			try {
				count = Util.S2I(cus_sn.substring(index, index+1));
			} catch (NumberFormatException e) {
				count = 0 ;
			}
			if (count > 9) {
				rwk_prd_cnt ++;
				oary5.setPrd_seq_id(prd_list.get(i).getPrd_seq_id());
				oary5.setRwk_cnt(count);
				oaryList.add(oary5);
			}
		}
		outTrx.setOary5(oaryList);
		outTrx.setOary5_cnt(rwk_prd_cnt);
		return lRc;
	}
	
	/**
	 * Only query box info
	 * 
	 * @return
	 */
	private long getBoxInfoByShipBox() {
		long lRc = _NORMAL;
		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			gszMsgBuf = "Input box id [" + inTrx.getBox_id() + "] is null!";
			return E_XPINQBOX_INVALID_INPUT_BOX_ID;
		}

        String hqlbox = "FROM Ret_box WHERE ship_box_id=?";
		Ret_box retboxship = retBoxDao.uniqueResult(hqlbox, inTrx.getBox_id());
        if(null != retboxship){
              outTrx.setBox_id(retboxship.getBox_id());
        }

		return lRc;
	}
	/**
	 *  query box info by pallet
	 *  by wfq
	 * @return
	 */
	private long sumBoxInfobyPallet(String palletId) {

		String woId, soId, prdStat, opeId, bnkFlg, lotId, opeVer, glot = null, oqc_skip_flg = " ",ext1="",boxId="";

		Map<String, Ret_wo> woMap = new HashMap<String, Ret_wo>();
		Map<String, Ret_box> boxMap = new HashMap<String, Ret_box>();
		Map<StatBoxOpeBnkWoMtrlKey, String> lotMap = new HashMap<StatBoxOpeBnkWoMtrlKey, String>();
		Map<StatBoxOpeBnkWoMtrlKey, Integer> rowMap = new HashMap<StatBoxOpeBnkWoMtrlKey, Integer>();
		Map<WoOpeKey, Integer> unLogofMap = new HashMap<WoOpeKey, Integer>();
		Map<String, Integer> finishedMap = new HashMap<String, Integer>();
		Map<String, Integer> unFinishedMap = new HashMap<String, Integer>();

		List<XpinqboxO1> oary1List = new ArrayList<XpinqboxO1>();
		List<XpinqboxO2> oary2List = new ArrayList<XpinqboxO2>();
		StringBuffer sql_box = new StringBuffer("");
		String hqlbox = "FROM Ret_box where pallet_id_fk='" + palletId + "'";
		
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		boxList = retBoxDao.find(hqlbox);
		if(boxList.size()==0){
			gszMsgBuf = "栈板 [" + inTrx.getPallet_id() + "] 中没有箱子!";
			return E_XPINQBOX_INVALID_INPUT_BOX_ID;
		}else{
			for(Ret_box retBox : boxList){
				sql_box.append("'"+retBox.getBox_id()+"',");
			}
			sql_box.deleteCharAt(sql_box.length()-1);
		}
		//wo prd cnt
		List<BoxInfoBeen> boxBeenList = new ArrayList<BoxInfoBeen>();
		String hqlWoPrdCnt = "SELECT SUM(PRD_QTY) C_PNL_CNT,WO_ID_FK FROM RET_BOX WHERE PALLET_ID_FK='"
				+ palletId + "' GROUP BY WO_ID_FK";
		List<Object[]> objList = retBoxDao.findBySQL(hqlWoPrdCnt);
		for (int i = 0; i < objList.size(); i++) {
			BoxInfoBeen boxInfoBeen = new BoxInfoBeen();
			Object boxInfo[] = objList.get(i);

			boxInfoBeen.setWo_id(boxInfo[1].toString());
		    boxInfoBeen.setC_pnl_cnt(Integer.valueOf(boxInfo[0].toString()));
			boxBeenList.add(boxInfoBeen);
		}
		//end
		String hql = "FROM Ret_prd_info where box_id_fk in ( " + sql_box + ")";
		if (inTrx.getWo_id() != null){
			hql += " and wo_id_fk = '" + inTrx.getWo_id() + "'"; 
		}

		List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.find(hql);
		for (Ret_prd_info retPrdInfo : retPrdInfoList) {
			boxId = retPrdInfo.getBox_id_fk();
			woId = retPrdInfo.getWo_id_fk();
			prdStat = retPrdInfo.getPrd_stat();
			opeId = "INPR".equals(prdStat) ? retPrdInfo.getCr_ope_id_fk() : retPrdInfo.getNx_ope_id_fk();
			String pathId = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_id_fk() : retPrdInfo.getCr_path_id_fk();
			String pathVer = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_ver_fk() : retPrdInfo.getCr_path_ver_fk();
			bnkFlg = retPrdInfo.getBnk_flg();
			lotId = retPrdInfo.getLot_id();
			// 统计在制数量,并确定行数
			StatBoxOpeBnkWoMtrlKey rowKey = new StatBoxOpeBnkWoMtrlKey(prdStat, opeId,
					boxId, bnkFlg, woId,retPrdInfo.getMtrl_box_id_fk());
			if (rowMap.containsKey(rowKey)) {
				rowMap.put(rowKey, rowMap.get(rowKey) + 1);
			} else {
				rowMap.put(rowKey, 1);
			}

			if (null != woId && !Util.isSpaceCheck(woId) && !woMap.containsKey(woId)) {
				Ret_wo retWo = retWoDao.get(woId);
				if (retWo != null) {
					woMap.put(woId, retWo);
				}
			}
			if (null != boxId && !Util.isSpaceCheck(boxId) && !boxMap.containsKey(boxId)) {
				Ret_box retBox = retBoxDao.get(boxId);
				if (null != retBox) {
					boxMap.put(boxId, retBox);
				}
			}

			if (null != lotId && !Util.isSpaceCheck(lotId)) {
				if (lotMap.containsKey(rowKey) && !lotId.equals(lotMap.get(rowKey))) {
					lotMap.put(rowKey, "MIX");
					glot = "MIX";
				} else {
					lotMap.put(rowKey, lotId);
					glot ="NOTMIX";
				}
			}
			XpinqboxO2 oary2 = new XpinqboxO2();
			oary2.setPrd_seq_id(retPrdInfo.getPrd_seq_id());
			oary2.setSlot_no(retPrdInfo.getSlot_no());
			if(null != retPrdInfo.getMtrl_slot_no_fk()){
				oary2.setMtrl_slot_no_fk(retPrdInfo.getMtrl_slot_no_fk());
			}
			oary2.setMdl_id(retPrdInfo.getMdl_id_fk());
			oary2.setOpe_id(opeId);
			oary2.setBnk_flg(bnkFlg);
			oary2.setPrd_stat(prdStat);
			oary2.setPrd_grade(retPrdInfo.getPrd_grade());
			oary2.setGroup_id(retPrdInfo.getGroup_id());
			//增加返工品标识
			Bis_pathiId id = new Bis_pathiId();
			id.setPath_id(pathId);
			id.setPath_ver(pathVer);
			Bis_pathi bis_pathi = bisPathiDao.get(id);
			if(bis_pathi !=null){
				if(bis_pathi.getPath_cate().equals(PATH_TYPE_REWK)){
					oary2.setOqc_skip_flg(_YES);
					oqc_skip_flg = "Y";
				}else{
					oary2.setOqc_skip_flg(_NO);
					oqc_skip_flg = "N";
				}
			}
			if(null!=retPrdInfo.getExt_1()){
				 oary2.setExt1(retPrdInfo.getExt_1());
				if(retPrdInfo.getExt_1().equals("N")){
				    ext1 = "N";
				}else{
					ext1 = "R";
				}
			}
			
//			if(!Util.isSpaceCheck(retPrdInfo.getOqc_skip_flg())){
//				oary2.setOqc_skip_flg(retPrdInfo.getOqc_skip_flg());
//				if (retPrdInfo.getOqc_skip_flg().equals("Y")) {
//					oqc_skip_flg = "Y";
//				}else if (retPrdInfo.getOqc_skip_flg().equals("N") && oqc_skip_flg == " " ){
//					oqc_skip_flg = "N";
//				}
//			}
			
			oary2.setMtrl_box_id(retPrdInfo.getMtrl_box_id_fk());
			oary2.setFab_sn(retPrdInfo.getFab_sn());
			oary2List.add(oary2);
		}
		Iterator<Entry<String, Ret_wo>> woIter = woMap.entrySet().iterator();
		while (woIter.hasNext()) {
			Entry<String, Ret_wo> entry = woIter.next();
			woId = entry.getKey();
			hql = "FROM Ret_prd_info where wo_id_fk = ? ";
			List<Ret_prd_info> retPrdInfoListByWo = retPrdInfoDao.list(hql,
					woId);
			for (Ret_prd_info retPrdInfo : retPrdInfoListByWo) {
				boxId = retPrdInfo.getBox_id_fk();
				prdStat = retPrdInfo.getPrd_stat();
				opeId = prdStat.equals("INPR") ? retPrdInfo.getCr_ope_id_fk()
						: retPrdInfo.getNx_ope_id_fk();
				bnkFlg = retPrdInfo.getBnk_flg();
				lotId = retPrdInfo.getLot_id();
				// 统计未出帐数量
				if ("INPR".equals(prdStat) || "WAIT".equals(prdStat)) {
					WoOpeKey unLogOfKey = new WoOpeKey(woId, opeId);
					if (unLogofMap.containsKey(unLogOfKey)) {
						unLogofMap.put(unLogOfKey,
								unLogofMap.get(unLogOfKey) + 1);
					} else {
						unLogofMap.put(unLogOfKey, 1);
					}
				}
				// 统计成品数量
				if ("SHIP".equals(prdStat) && "1".equals(bnkFlg)) {
					if (finishedMap.containsKey(woId)) {
						finishedMap.put(woId, finishedMap.get(woId) + 1);
					} else {
						finishedMap.put(woId, 1);
					}
				}
			}
			// 统计半成品数量
			if (!unFinishedMap.containsKey(woId)) {
				unFinishedMap.put(woId, fmtInt(woMap.get(woId).getRl_prd_qty())
						- fmtInt(finishedMap.get(woId)));
			}

		}
		Iterator<Entry<WoOpeKey, Integer>> testIt = unLogofMap.entrySet()
				.iterator();
		while (testIt.hasNext()) {
			Entry<WoOpeKey, Integer> testEntry = testIt.next();
			logger.info(testEntry.getKey());
			logger.info(testEntry.getValue());
		}
		// 统计该站已完成数量:列出所有的站点，去HIS中 count(*)

		Iterator<Entry<StatBoxOpeBnkWoMtrlKey, Integer>> rowIter = rowMap
				.entrySet().iterator();
		while (rowIter.hasNext()) {
			Entry<StatBoxOpeBnkWoMtrlKey, Integer> entry = rowIter.next();
			StatBoxOpeBnkWoMtrlKey rowKey = entry.getKey();
			logger.info(rowKey);
			boxId = rowKey.getBox_id();
			woId = rowKey.getWo_id();
			opeId = rowKey.getOpe_id();
			prdStat = rowKey.getPrd_stat();
			bnkFlg = rowKey.getBnk_flg();
			Ret_wo retWo = woMap.get(woId);
			if (null != retWo) {
				XpinqboxO1 oary1 = new XpinqboxO1();
				oary1.setCus_id(retWo.getCus_id_fk());
				soId = retWo.getSo_id();
				if (null != soId && !"".equals(soId)) {
					Ret_wo retSo = retWoDao.get(retWo.getSo_id());
					if (null != retSo) {
						oary1.setSo_id(retWo.getSo_id());
					}
				}
				Ret_box retBox = retBoxDao.get(boxId);
				String hqldest="";
				if(!Util.isSpaceCheck(retBox.getDest_shop())){
					if(!Util.isSpaceCheck(retBox.getBox_def())){
						 hqldest = "FROM Bis_data WHERE data_cate = 'DEST' AND data_id = '" + retBox.getBox_def() + "'";
					}else{
				         hqldest = "FROM Bis_data WHERE data_cate = 'DEST' AND data_id = '" + retBox.getDest_shop() + "'"; 
					}
				List<Bis_data> dataList = bisDataDao.find(hqldest);
				if(dataList.size()>0){
				   oary1.setDes_data_desc(dataList.get(0).getData_desc());
				}
				}
				if(!Util.isSpaceCheck(retBox.getShip_box_id())){
					oary1.setShip_box_id(retBox.getShip_box_id());
				}
				if(!Util.isSpaceCheck(retBox.getPallet_id_fk())){
					oary1.setPallet_id(retBox.getPallet_id_fk());
				}
				oary1.setWo_id(woId);
				oary1.setMtrl_box_id_fk(rowKey.getMtrl_box_id());
				oary1.setMdl_id(retWo.getMdl_id_fk());
				oary1.setBox_stat((boxId != null && !"".equals(boxId)) ? retBox
						.getBox_stat() : "");
				if(glot.equals("MIX")){
					oary1.setLot_id(glot);
				}else{
					oary1.setLot_id(lotMap.get(rowKey));
				}
				oary1.setBox_id(boxId);
				oary1.setPrd_qty(null == retBox ? null : retBox.getPrd_qty());
				oary1.setWip_qty(null == retBox ? null : retBox.getPrd_qty());
				// oary1.setWip_qty(fmtInt(entry.getValue()));
				oary1.setFinished_qty(fmtInt(finishedMap.get(woId)));
				oary1.setUn_finished_qty(fmtInt(unFinishedMap.get(woId)));
				oary1.setPrd_stat(prdStat);
				oary1.setOpe_id(opeId);
				oary1.setBnk_flg(bnkFlg);
				oary1.setUn_logof_qty(fmtInt(unLogofMap.get(new WoOpeKey(woId,
						opeId))));
				oary1.setOqc_skip_flg(oqc_skip_flg);
				oary1.setExt1(ext1);
				logger.info(oary1.getUn_logof_qty());
				// oary1.setLogof_qty(logof_qty) TODO:等待HIS_RET_PRD_INFO表完成后再完善
				oary1List.add(oary1);
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary1(oary1List);
		outTrx.setOary1_cnt(oary1List.size());
		outTrx.setOary2(oary2List);
		outTrx.setOary2_cnt(oary2List.size());
		outTrx.setBox_info(boxBeenList);
		return 0;
	}
}
